<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/7/24
 * Time: 10:32
 */

namespace app\api\controller;

use think\Db;
use think\Controller;

class Token extends Controller
{
    public function index(){
        //检查是否post
        if(request()->isPost()){
            $date=request()->post();
            $where['apiuser_name']=$date['user_name'];
            $where['apiuser_pwd']=$date['user_pwd'];
            $user=Db::name('apiuser')->where($where)->find();
            if($user){
                if(time()-$user['updatetime']>=7200){
                    $string=$date['user_pwd'].$date['user_name'].$date['user_pwd'].time();
                    $md5string=md5($string);
                    $update['token']=$md5string;
//                    echo $md5string;die;
                    $update['updatetime']=time();

                    $changetoken=Db::name('apiuser')->where($where)->update($update);

                    if($changetoken){
                        $resmsg['code']=0;
                        $resmsg['msg']="获取成功";
                        $resmsg['data']['token']=$md5string;
                    }else{
                        $resmsg['code']=-1;
                        $resmsg['msg']="获取失败";
                    }
                }else{
                    $resmsg['code']=0;
                    $resmsg['msg']="获取成功";
                    $resmsg['data']['token']=$user['token'];

                }
                return json_encode($resmsg);
            }else{
                $resmsg['code']=103;
                $resmsg['msg']="账号或密码错误";
                return json_encode($resmsg);
            }
        }else{
            $resmsg['code']=101;//非post请求
            $resmsg['msg']="非post请求";//非post请求
            return json_encode($resmsg);
        }
    }




}